import {
  applyDecorators,
  createParamDecorator,
  ExecutionContext,
  SetMetadata,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';

export const RequireLoginKey = 'require-login';

export const RequrieLogin = () => SetMetadata(RequireLoginKey, true);

export const GetUserInfo = createParamDecorator(
  (data: string, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    return data ? request.user[data] : request.user;
  },
);

export const uploadsFilter = (name = 'file', op: MulterOptions = {}) => {
  return applyDecorators(UseInterceptors(FileInterceptor(name, op)));
};
